package com.wey.springboot.util;

import lombok.Data;

@Data
public class CommonResult<T> {
  private long code;
  private String msg;
  private T data;

  public CommonResult(ResultCode resultCode, T data) {
    this.code = resultCode.getCode();
    this.msg = resultCode.getMsg();
    this.data = data;
  }

  public CommonResult(long code, String msg, T data) {
    this.code = code;
    this.msg = msg;
    this.data = data;
  }

  public static <T> CommonResult<T> success() {
    return new CommonResult<T>(ResultCode.SUCCESS, null);
  }

  public static <T> CommonResult<T> success(T data) {
    return new CommonResult<T>(ResultCode.SUCCESS, data);
  }

  public static <T> CommonResult<T> success(T data, String msg) {
    return new CommonResult<T>(ResultCode.SUCCESS.getCode(), msg, data);
  }

  public static <T> CommonResult<T> successMsg(String msg) {
    return new CommonResult<T>(ResultCode.SUCCESS.getCode(), msg, null);
  }

  public static <T> CommonResult<T> fail() {
    return new CommonResult<T>(ResultCode.ERROR, null);
  }

  public static <T> CommonResult<T> fail(String msg) {
    return new CommonResult<T>(ResultCode.ERROR.getCode(), msg, null);
  }

  public static <T> CommonResult<T> fail(long code, String msg) {
    return new CommonResult<T>(code, msg, null);
  }
}
